package com.hgm.travelapp.activity


import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.*
import androidx.fragment.app.FragmentManager
import com.hgm.travelapp.R
import com.hgm.travelapp.databinding.ActivityMainBinding
import com.hgm.travelapp.fragment.HomeFragment
import com.hgm.travelapp.fragment.OrderFragment
import com.hgm.travelapp.fragment.PersonFragment
import com.hgm.travelapp.fragment.TicketingFragment

class MainActivity : AppCompatActivity() {

    private val mBinding: ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }


    private final val fm: FragmentManager = supportFragmentManager
    private val home = HomeFragment()
    private val person = PersonFragment(home)
    private val ticket = TicketingFragment()
    private val order = OrderFragment()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //隐藏状态栏
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        setContentView(mBinding.root)

        initView()
        initEvent()
    }

    private fun initEvent() {
        mBinding.nav.setNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.menu_person -> {
                    fm.beginTransaction().replace(R.id.container, person).addToBackStack(null)
                        .commit()
                }
                R.id.menu_server -> {
                    fm.beginTransaction().replace(R.id.container, ticket).addToBackStack(null)
                        .commit()
                }
                R.id.menu_order -> {
                    fm.beginTransaction().replace(R.id.container, order).addToBackStack(null)
                        .commit()
                }
                R.id.menu_exit -> {
                    this.finish()
                    startActivity(Intent(this, LoginActivity::class.java))
                }
            }

            //控制菜单的打开和关闭
            mBinding.drawer.closeDrawers()
            true
        }

    }


    private fun initView() {
        //默认加载主fragment
        fm.beginTransaction().replace(R.id.container, home).commit()
        //还原抽屉布局的icon颜色
        mBinding.nav.itemIconTintList = null
    }

}


